package com.blog.blogservice.api.controller.v1;

import com.blog.blogservice.app.service.ArticleService;
import com.blog.blogservice.domain.entity.Article;
import com.blog.blogservice.domain.entity.BaseResponse;
import com.blog.blogservice.domain.entity.Category;
import com.blog.blogservice.domain.entity.Label;
import com.blog.blogservice.domain.entity.Results;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * 文章表(Article)表控制层
 *
 * @author guanglin.ma
 * @since 2024-03-02 23:24:54
 */

@RestController("articleController.v1")
@RequestMapping("/v1/articles")
@RequiresPermissions("admin")
public class ArticleController {


    @Autowired
    private ArticleService articleService;


    ObjectMapper objectMapper = new ObjectMapper();

    @PostMapping("/upload-article")
    public BaseResponse handleUploadArticle(@RequestParam("file") MultipartFile file,
                                            @RequestParam("articleTitle") String articleTitle,
                                            @RequestParam("tips") String tips,
                                            @RequestParam("labelList") String labelList,
                                            @RequestParam("categoryList") String categoryList) throws Exception {
        // 检查文件是否为空
        if (file.isEmpty()) {
            return BaseResponse.error("Please select a file to upload");
        }
        Article article = new Article();
        article.setArticleTitle(articleTitle);
        article.setTips(tips);
        article.setLabelList(objectMapper.readValue(labelList, new TypeReference<List<Label>>() {
        }));
        article.setCategoryList(objectMapper.readValue(categoryList, new TypeReference<List<Category>>() {
        }));
        BaseResponse response = new BaseResponse("20000",
                this.articleService.addArticle(file, article));
        return response;


    }

    /**
     * 删除文章 物理删除
     *
     * @return
     */
    @DeleteMapping("/delete/article/{articleId}")
    public ResponseEntity<String> deleteArticle(@PathVariable("articleId") Integer articleId) throws ExecutionException, InterruptedException {
        articleService.deleteArticle(articleId);
        return Results.success();
    }

    /**
     * 文章列表
     *
     * @return
     */
    @GetMapping("/list/article")
    public BaseResponse<List<Article>> articleList(Article article) {
        List<Article> articleList = articleService.listArticle(article);

        BaseResponse<List<Article>> response = new BaseResponse<>("20000", articleList);
        return response;
    }

}

